/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.base.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dian.common.validator.ValidatorUtils;
import com.dian.common.validator.group.AddGroup;
import com.dian.common.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.Query;
import com.dian.common.server.CommonService;
import com.dian.modules.base.dao.CustomerDao;
import com.dian.modules.base.entity.CustomerEntity;
import com.dian.modules.base.service.CustomerService;

/**
 * 客户信息服务实现类
 *
 * @author xiaolai
 * @email xiaolai@9dyun.cn
 * @date 2019-05-22 01:47:19
 */
@Service("baseCustomerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {
    @Autowired
    public CommonService commonService;

    @Override
    public PageUtils balanceList(Map<String, Object> params) {
        IPage<CustomerEntity> page = this.page(new Query<CustomerEntity>().getPage(params), getQueryWrapper(params));
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        IPage<CustomerEntity> page = this.page(new Query<CustomerEntity>().getPage(params), getQueryWrapper(params));

        return new PageUtils(page);
    }

    @Override
    public boolean saveInfo(CustomerEntity baseCustomerEntity) {

        //数据完整性校验
        this.checkParams(baseCustomerEntity, AddGroup.class);

        //设置编码等其它属性
        //baseCustomerEntity.setOrderNo(commonService.getOrderCode("XSD"));


        //保存
        this.save(baseCustomerEntity);
        return true;
    }


    @Override
    public boolean updateInfo(CustomerEntity baseCustomerEntity) {

        //数据完整性校验
        this.checkParams(baseCustomerEntity, UpdateGroup.class);

        //更新
        this.updateById(baseCustomerEntity);

        return true;
    }

    @Override
    public boolean deleteInfo(Long[] idList) {

        //删除状态校验
        this.deleteCheckState(idList);

        this.removeByIds(Arrays.asList(idList));

        return true;
    }


    /***********************************************************************************************/
    /****************************************** 私有方法 ********************************************/
    /***********************************************************************************************/
    /**
     * 删除状态校验
     *
     * @param
     */
    private void deleteCheckState(Long[] idList) {
        for (int index = 1; index <= idList.length; index++) {
            CustomerEntity baseCustomerEntity = this.getById(idList[index - 1]);
            /*
            if(!baseCustomerEntity.getOrderState().equals(OrderHead_OrderStateEnum.WAITCHECK)){
                throw new RRException(String.format("第%s行中为已审核的订单,不允许删除",index));
            }*/
        }


    }

    /**
     * 参数校验
     *
     * @param baseCustomerEntity
     * @param cls
     */
    private void checkParams(CustomerEntity baseCustomerEntity, Class<?> cls) {

        //实体类参数校验
        ValidatorUtils.validateEntity(baseCustomerEntity, cls);

        //其它业务参数校验

    }

    private QueryWrapper<CustomerEntity> getQueryWrapper(Map<String, Object> params) {
        /*QueryWrapper<SupplierEntity> queryWrapper=new QueryWrapper<>();*/
        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.isBlankIfStr(params.get("customerCode"))) {
            queryWrapper.likeLeft("customer_code",params.get("customerCode"));
        }
        if (!StrUtil.isBlankIfStr(params.get("customerName"))) {
            queryWrapper.likeLeft("customer_name",params.get("customerName"));
        }
        queryWrapper.orderByAsc("id");
        return queryWrapper;
    }
}
